package club.easin.product.controller;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.mockito.internal.matchers.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import club.easin.product.service.ProductService;
import club.easin.util.bean.ReturnData;
import club.easin.util.bean.UdReturnData;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("product")
@CrossOrigin(allowCredentials="true")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	private static final int SUCCESS=0;
	private static final int NO_SUCCESS=-1;
	
	//测试数据库是否链接
	@RequestMapping(value="getTwoProduct",method=RequestMethod.POST,produces="application/json;charset=utf-8;")
	@ResponseBody
	public Object getTwoProduct(@RequestBody Map<String,Object> map){
		Map<String,Object> parms=(Map<String, Object>) map.get("extra");
		return this.productService.getTwoProduct();
	}
	
	/**根据产品id获取产品属性*/
	@RequestMapping("getProductByid")
	@ResponseBody
	public Object getProductByid(String productId){
		ReturnData data=new ReturnData();
		try {
			List<Map<String,Object>> list=this.productService.getProductByid(productId);
			data.setCode(200);
			data.setDesc("productInfo");
			data.setResult(list);
		} catch (Exception e) {
			// TODO: handle exception
			data.setCode(200);
		}
		
		return data;
	}
	
  
	/**根据产品id和产品的属性名称获取产品的属性值（任务推送）*/
	@RequestMapping(value="getPropertValue",method=RequestMethod.POST,produces="application/json;charset=utf-8;")
	@ResponseBody
	public Object getPropertValue(@RequestBody Map<String,Object> map){
		UdReturnData uData=new UdReturnData();
		try {
		Map<String,Object> paramMap=(Map<String, Object>) map.get("variables");
		String productId=(String)paramMap.get("product");
		String property=(String)paramMap.get("chara");
		List<String> list=this.productService.getPropertyByid(productId, property);
		String chara=productId+"的"+property+":";
		String value="";
		for (String pro : list) {
			value=value+pro;
		}
		
		uData.setResCode(SUCCESS);
		Map<String,Object> map2=new HashMap<String, Object>();
		map2.put("msgType", "product");
		map2.put("msgContent", chara+value);
		uData.setMessage(map2);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			uData.setResCode(NO_SUCCESS);
			uData.setMessage(null);
		}
		return uData;
		
	}
	
	/**网易七鱼根据产品id和产品属性名称查询产品属性值
	 * @throws IOException */
	@RequestMapping(value="qyGetProductProtuct",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void qyGetProductProtuct(String userId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String pString=request.getParameter("params");
	    JSONObject jp=JSONObject.fromObject(pString);
		String productid=jp.getString("ProductID");
		String chara=jp.getString("Chara");
		ReturnData data=new ReturnData();
		Map<String,Object> map=new HashMap<String, Object>();
		try {
		if(productid!=""&&chara!=""&&chara!=null&&productid!=null){
			data.setCode(200);
			data.setDesc("chara");
			
			List<String> list=this.productService.getPropertyByid(productid, chara);
			if(list.size()>0){
			String value=productid+"的"+chara+":";
			//map.put("chara",value);
			map.put("charaList",list);
			}else{
				map.put("chara", "哎呀！我还不知道哟，可以转人工客服");
			}
			data.setResult(map);
		}else{
			data.setCode(500);
			data.setDesc("chara");
			map.put("chara","查询失败，请您重新问答");
			data.setResult(map);
		}
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setDesc("error");
			map.put("chara","系统错误查询失败");
			data.setResult(map);
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer = response.getWriter();
		JSONObject datajson=JSONObject.fromObject(data);
		writer.write(datajson.toString());
	}
	
	/**网易七鱼根据产品名称和产品阶段查找阶段使用的软件
	 * @throws IOException */
	@RequestMapping(value="qyGetSoftware",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void qyGetSoftware(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String param=request.getParameter("params");
		JSONObject jb=JSONObject.fromObject(param);
		Map<String,Object> paramMap=(Map<String, Object>)jb;
		String productName=(String)paramMap.get("productName");
		String stage=(String)paramMap.get("stage");
		ReturnData data=new ReturnData();
		Map<String,Object> map=new HashMap<String, Object>();
		try {
			data.setCode(200);
			data.setDesc("product_stage");
			if(productName!=null&&!"".equals(productName)){
				List<Map<String,Object>> list=this.productService.getsoftwareByproduct(productName, stage);
				map.put("software", list);
			}else{
				map.put("software", "请您再输入一次产品名哟");
			}
			data.setResult(map);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			map.put("software", "查询失败");
			data.setResult(map);
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
	}
	
	/**根据故障现象查找故障名称和故障检查方法
	 * @throws Exception */
	@RequestMapping(value="getWayByPhenomenon",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getWayByPhenomenon(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> paramMap=(Map<String, Object>)object;
		String phenomenon=(String)paramMap.get("Phenomenon");
		ReturnData data=new ReturnData();
		Map<String,Object> map=new HashMap<String, Object>();
		try {
			data.setCode(200);
			data.setDesc("phenomenon");
			if(phenomenon!=null&&!"".equals(phenomenon)){
				Map<String,Object> pMap=this.productService.getInspectByPhenomenon(phenomenon);
				map.put("inspect", pMap);
			}else{
				map.put("inspect", "请您再描述一下现象哟");
			}
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			map.put("inspect","查询失败");
			data.setResult(map);
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	/**根据故障现象和产品名称查找原因和解决办法
	 * @throws Exception */
	@RequestMapping(value="getReasonAndway",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getReasonAndway(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> pMap=(Map<String, Object>)object;
		String productName=(String)pMap.get("productName");
		String type=(String)pMap.get("type");
		ReturnData data=new ReturnData();
		Map<String,Object> map=new HashMap<String, Object>();
		try {
			data.setCode(200);
			data.setDesc("ReasonAndway");
			List<Map<String,Object>> list=this.productService.getReasonAndWay(productName, type);
			map.put("rw",list);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			map.put("rw", "查询失败");
			data.setResult(map);
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	/**根据产品系列查找产品样本
	 * @throws Exception */
	@RequestMapping(value="getSample",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getSample(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> pMap=(Map<String, Object>)object;
		String range=(String)pMap.get("range");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("sample");
			Map<String,Object> map=this.productService.getSampleByRange(range);
			if(map==null||map.isEmpty()){
				map=new HashMap<String, Object>();
				map.put("noHas","暂时没找到哟，可咨询人工客服");
			}
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
	}
	
	/**根据产品id查找产品样本
	 * @throws Exception */
	@RequestMapping(value="getSampleByProductid",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getSampleByProductid(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> pMap=(Map<String, Object>)object;
		String productid=(String)pMap.get("ProductID");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("sample");
			Map<String,Object> map=this.productService.getSampleByProductid(productid);
			if(map==null||map.isEmpty()){
				map=new HashMap<String, Object>();
				map.put("noHas","暂时没有找到哟可咨询人工客服");
			}
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
	}
	
	/**根据产品id查找替换产品
	 * @throws Exception */
	@RequestMapping(value="getReplace",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getReplace(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> pMap=(Map<String, Object>)object;
		String productID=(String)pMap.get("ProductID");
		ReturnData data=new ReturnData();
		try {
			data.setCode(200);
			data.setDesc("replace");
			Map<String,Object> map=this.productService.getReplaceByProductID(productID);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	
	/**根据根据产品名称和故障名称查找故障排除列表
	 * @throws Exception */
	@RequestMapping(value="getPhenomenon",method=RequestMethod.POST,produces="application/x-www-form-urlencoded;charset=UTF-8")
	public void getPhenomenon(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String param=request.getParameter("params");
		JSONObject object=JSONObject.fromObject(param);
		Map<String,Object> pMap=(Map<String, Object>)object;
		String productName=(String)pMap.get("productName");
		String faultName=(String)pMap.get("faultName");
		ReturnData data=new ReturnData();
		Map<String,Object> map=new HashMap<String, Object>();
		try {
			data.setCode(200);
			data.setDesc("type");
			List<String> list=this.productService.getPhenomenonList(productName, faultName);
			map.put("typeList", list);
			data.setResult(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			data.setCode(500);
			data.setResult("");
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter writer=response.getWriter();
		JSONObject jsonObject=JSONObject.fromObject(data);
		writer.write(jsonObject.toString());
		
	}
	

}
